package com.dense.kuiniu.floatcabinet.mvp.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.di.component.DaggerAdminComponent;
import com.dense.kuiniu.floatcabinet.di.module.AdminModule;
import com.dense.kuiniu.floatcabinet.mvp.contract.AdminContract;
import com.dense.kuiniu.floatcabinet.mvp.event.OpenDoorEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.ScanEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.TaskInfoEvent;
import com.dense.kuiniu.floatcabinet.mvp.manager.bord.Door;
import com.dense.kuiniu.floatcabinet.mvp.presenter.AdminPresenter;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BoxForAdminAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogMessage;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.EventBusManager;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/10/28
 */
public class AdminActivity extends ImmerseActivity<AdminPresenter> implements AdminContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @Inject
    BoxForAdminAdapter adminAdapter;
    private DialogMessage mDialogMessage;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAdminComponent.builder().appComponent(appComponent)
                .adminModule(new AdminModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_admin;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                mDialogMessage = new DialogMessage(AdminActivity.this);
                StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
                mRecyclerView.setLayoutManager(manager);
                mRecyclerView.setAdapter(adminAdapter);
                adminAdapter.addChildClickViewIds(R.id.layout_door);
                adminAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                        Door door = (Door) adapter.getItem(position);
                        if (door != null) {
                            EventBusManager.getInstance().post(new OpenDoorEvent("123", 2, door.getDoorNumber()));
                        }
                    }
                });
                if (mPresenter != null) {
                    mPresenter.queryDoor();
                }
            }
        });

    }

    @OnClick({R.id.tv_back, R.id.btn_scan})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.btn_scan:
                scan();
                break;
        }
    }

    @Override
    public void showMessage(@NonNull String message) {

    }

    @Override
    public void showDoorData(List<Door> doors) {
        if (adminAdapter != null) {
            adminAdapter.setDoors(doors);
        }
    }

    private void scan() {
        EventBusManager.getInstance().post(new ScanEvent("123", 1, 2));
    }


    public void showInfo(String msg) {
        Log.e("接到消息", "msg");
        mDialogMessage.justInfo(msg).show();
    }

    @Subscribe()
    public void taskInfoEvent(TaskInfoEvent event) {
        showInfo("门号:" + event.doorNumber + "\n动作:" + event.action);
    }

}